package interview;

/**
 * @Author: zhangrongqing
 * @Date: 2019/11/11 15:09
 */
/*
 * 原子可见性
 */
public class TestVolidate {
    public static void main(String[] args) throws InterruptedException {
        ThreadDemo threadDemo = new ThreadDemo();
        Thread thread = new Thread(threadDemo);
        thread.start();
        while (true) {
//            Boolean flagResult = threadDemo.getFlag();
//            System.out.println("flag result:"+threadDemo.getFlag());
            if (threadDemo.getFlag()) {
                Thread.sleep(300);
                System.out.println("Main class get flag vlaue!!!");
                break;
            }
        }
    }
}

class ThreadDemo implements Runnable {
    public Boolean flag = false;

    @Override
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        flag = true;
        System.out.println("Class:ThreadDemo flag is " + flag);
    }

    public Boolean getFlag() {
        return this.flag;
    }
}
